package com.android.controller_wifi;

import com.android.controller_wifi.HttpVideo.Callback;
import com.wang.controller_wifi.manager.NotifyListener;
import com.wang.controller_wifi.manager.SimpleNotifyListener;
import com.wangbl.manager.utils.Logger;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends BaseActivity implements OnTouchListener {

	private static final String TAG = "MainActivity";
	private SurfaceView surface;
	private SurfaceHolder holder;
	private HttpVideo video;
	private GridView gridView;
	private int[] resId = { R.drawable.ic_a, R.drawable.ic_b, R.drawable.ic_c, R.drawable.ic_d, R.drawable.ic_e,
			R.drawable.ic_f, R.drawable.ic_g, R.drawable.ic_h, R.drawable.ic_xing, R.drawable.ic_i, R.drawable.ic_j,
			R.drawable.ic_jing };

	// m 对应 * n对应#
	private String[] cmds = { "a", "b", "c", "d", "e", "f", "g", "h", "m", "i", "j", "n" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		surface = (SurfaceView) findViewById(R.id.surfaceview);
		gridView = (GridView) findViewById(R.id.gridview);
		
		ImageView left = (ImageView) findViewById(R.id.controller_left_btn);
		ImageView right = (ImageView) findViewById(R.id.controller_right_btn);
		ImageView up = (ImageView) findViewById(R.id.controller_up_btn);
		ImageView down = (ImageView) findViewById(R.id.controller_down_btn);
		
		left.setOnTouchListener(touchListener);
		right.setOnTouchListener(touchListener);
		up.setOnTouchListener(touchListener);
		down.setOnTouchListener(touchListener);

		holder = surface.getHolder();
		gridView.setAdapter(new MyAdapter());

		tv_pd.setText("正在获取视频信号...");
		video = new HttpVideo(holder);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		pd.show();
		video.run();
		video.setCallback(new Callback() {
			@Override
			public void onCallback() {
				pd.dismiss();
			}
		});

		video.setCallback2(new Callback() {

			@Override
			public void onCallback() {
				connectFailureAlert();
			}
		});
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return resId.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView view = new ImageView(MainActivity.this);
			view.setImageResource(resId[position]);
			view.setTag(position);
			view.setClickable(true);
			view.setOnTouchListener(MainActivity.this);
			return view;
		}
	}

	private OnTouchListener touchListener = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				handleDown(v.getId());
				break;
			case MotionEvent.ACTION_UP:
				manager.stop();
				break;
			}
			return false;
		};
	};
	
	private void handleDown(int id) {
		switch (id) {
		case R.id.controller_left_btn:
			manager.left();
			break;
		case R.id.controller_right_btn:
			manager.right();
			break;
		case R.id.controller_up_btn:
			manager.drive();
			break;
		case R.id.controller_down_btn:
			manager.back();
			break;

		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			String c = cmds[(Integer) v.getTag()];
			if ("i".equals(c) || "j".equals(c)) {
				manager.sendCmd(c);
			}
			break;
		case MotionEvent.ACTION_UP:
			String c2 = cmds[(Integer) v.getTag()];
			if ("i".equals(c2) || "j".equals(c2)) {
				manager.interrupt();
			} else {
				manager.sendCmd(c2);
			}
			break;
			
		case MotionEvent.ACTION_CANCEL:
			String c3 = cmds[(Integer) v.getTag()];
			if ("i".equals(c3) || "j".equals(c3)) {
				manager.interrupt();
			}
			break;
		}
		return false;
	}

	@Override
	protected void onStart() {
		super.onStart();
		manager.register(listener);
	}

	@Override
	protected void onStop() {
		super.onStop();
		manager.unregister(listener);
	}

	private NotifyListener listener = new SimpleNotifyListener() {

		@Override
		public void onConnectLose() {
			connectFailureAlert();
		};
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (video != null) {
			video.destory();
			video = null;
		}
	}

	private void connectFailureAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示ʾ").setMessage("连接出错，请检查wifi设置...")
				.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Logger.w(TAG, "OK");
						Intent intent = new Intent(
								Settings.ACTION_WIFI_SETTINGS);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					}
				}).setCancelable(false);
		AlertDialog locationDialog = builder.create();
		locationDialog.show();
	}

}
